package westh.ilib.struts.action.manage.conference;

import westh.ilib.util.PageInfo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;
import westh.ilib.bean.manage.ConferenceBean;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.List;
import westh.config.Config;
/**
 * @author zhangjian
 * 学术会议
 * modify by :CaoNingbo
 * date :2010-09-25 pm 14:32
 * content:add pagination function
 */

public class GetAcademicMeetingAction extends Action
{
	private final static Log log = LogFactory.getLog(GetAcademicMeetingAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
    
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) 
	{
		//得到输入的Json-串
		DynaActionForm f = (DynaActionForm) form;
		String conferenceName = f.getString("conferenceName");
		JSONObject inJson = new JSONObject();
		JSONObject inStr = new JSONObject();
		inStr.put("conferenceName", conferenceName);
		inJson.put("methodName", "getAcademicMeeting");
		inJson.put("jsonStr", inStr);
		inJson.put("conferenceName", conferenceName);
		String inputStr = inJson.toString();
		//jSon-String
		String outString = "";
		
		/**************获取jSon信息 start***************************/
		try
		{
			MuleClient client = new MuleClient();
			//调用WebService remoteUrl 4381 
			UMOMessage umo = client.send("axis:http://"+remoteUrl+":4381/LIBJson/iDlibJson?method=rulesControl",inputStr,null);
			outString = (String)umo.getPayload();
			log.debug("umo--------------------------------------"+outString);
		} catch (Exception e) 
		{
			throw new RuntimeException(e);
		}
		/**************获取jSon信息 end***************************/

		String retCode=""; //返回代码
		String retMsg="";//返回结果信息
		String returnStr="";//跳转信息
		
		/****************解析输出jSon串   start*********************/
		
		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		ConferenceBean conference=null;
		
		// dispatch page related begin
        String strPageNo = (String)request.getParameter("pageNo");
        String strPageSize = (String)request.getParameter("pageSize");
        // dispatch page related end
        
        //display pagesize
        log.debug("strPagesize="+strPageSize);
		
		if (retCode.equals("0100"))
		{

			String result = jsonOut.getString("result");
			log.debug("in ilib result =" + result);
			JSONArray resultArry = JSONArray.fromObject(result);
			log.info("JSONArray="+resultArry.size());
			
			//解析数组
			conference = new ConferenceBean();
			List list = new ArrayList();

	    	for(int i=0;i<resultArry.size();i++)
	    	{
	    		conference = new ConferenceBean();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				conference.setConferenceId(jsonOut2.getString("conferenceId"));
				//add by CaoNingbo
				conference.setCategory(jsonOut2.getString("category"));
				conference.setConferenceName(jsonOut2.getString("conferenceName"));
				//conference.setConferenceFullName(jsonOut2.getString("conferenceFullName"));
				conference.setConferenceDateStart(jsonOut2.getString("conferenceDateStart"));
				conference.setConferenceDateEnd(jsonOut2.getString("conferenceDateEnd"));
				conference.setConferenceVenue(jsonOut2.getString("conferenceVenue"));
				conference.setConferenceDeadline(jsonOut2.getString("conferenceDeadline"));
				
				list.add(conference);
			}
	    	/***************dispatch page related begin ****************/
			 
			PageInfo pageInfo = new PageInfo();
			int iPageNo = pageInfo.getPageNoFromJSP(strPageNo);
			int iPageSize = pageInfo.getPageSizeFromJSP(strPageSize);
			log.debug("iPageSize = " + iPageSize);
			
            pageInfo.setMaxCount(list.size());
            pageInfo.setPageNO(iPageNo);
            pageInfo.setPageSize(iPageSize);
            
            List outputList=null;
            
            int endCount=pageInfo.getMaxCount();
            
            if(endCount>iPageSize*iPageNo)
            {
            	endCount=iPageSize*iPageNo;
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            else if(endCount<iPageSize*iPageNo&&endCount>iPageSize * (iPageNo - 1))
            {
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            else
            {
            	iPageNo=1;
            	pageInfo.setPageNO(iPageNo);
            	if(endCount>iPageSize*iPageNo)
            		endCount=iPageSize*iPageNo;
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            pageInfo.setPageList(outputList);
            request.setAttribute("pageInfo", pageInfo);
            request.setAttribute("pageNo", strPageNo);
            request.setAttribute("pageSize", strPageSize);
			/*****************dispatch page related  end *****************/
			
			
			/****************解析输出jSon串   end***************************/
			log.debug("list = " + list);
			log.debug("in lib request =" + request);
			return mapping.findForward("succ");
			
		}
		if (retCode.equals("0501"))
		{
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		if (retCode.equals("0502")) 
		{
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		return mapping.findForward(returnStr);
	}

}
